package com.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.sfzy.entity.Certificate;
import com.sfzy.service.CertificateService;
import com.sfzy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("certificate")
public class CertificateController {
    @Autowired
    private CertificateService certificateService;
    //增加
    @PostMapping("add")
    public R insertCertificate(@RequestBody Certificate certificate) {
        int num = certificateService.insertCertificate(certificate);
        return R.ok(num);
    }
    //删除
    @DeleteMapping("{id}")
    public R deleteCertificate(@PathVariable("id") int id) {
        int num = certificateService.deleteCertificate(id);
        return R.ok(num);
    }
    //修改
    @PutMapping("update")
    public R updateCertificate(@RequestBody Certificate certificate) {
        int num = certificateService.updateCertificate(certificate);
        return R.ok(num);
    }
    //根据id查询
    @GetMapping("{id}")
    public R getCertificate(@PathVariable("id") int id) {
        Certificate certificate = certificateService.selectCertificateById(id);
        return R.ok(certificate);
    }
    //分页查询
    @GetMapping("page")
    public R getCertificates(Certificate certificate,
                             @RequestParam(required = false,defaultValue = "1")int pageNum,
                             @RequestParam(required = false,defaultValue = "3")int pageSize) {
        PageInfo<Certificate> certificatePageInfo = certificateService.selectAllCertificates(certificate, pageNum, pageSize);
        return R.ok(certificatePageInfo);
    }
}
